package com.zry.controller.collect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zry.VO.CollectVo;
import com.zry.entity.CollectCategory;
import com.zry.service.collectCategory.ICollectCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Zry
 */
@RestController
public class CollectCategoryController {

    @Autowired
    private ICollectCategoryService collectCategoryService;

    @GetMapping("/api/collectCategory/load")
    @ResponseBody
    public List<Map<String, Object>> getCollectCategoryList() {
        LambdaQueryWrapper<CollectCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(CollectCategory::getId, CollectCategory::getCategoryTitle);
        queryWrapper.eq(CollectCategory::getStatus, 1);

        List<CollectCategory> collectCategoryList = collectCategoryService.list(queryWrapper);

        if (!CollectionUtils.isEmpty(collectCategoryList)) {
            return collectCategoryList.stream().map(collectCategory -> {
                Map<String, Object> map = new HashMap<>();
                map.put("id", collectCategory.getId());
                map.put("categoryTitle", collectCategory.getCategoryTitle());
                return map;
            }).collect(Collectors.toList());

        } else {
            return new ArrayList<>();
        }
    }
}